package net.chasing.androidbaseconfig.util;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.ColorInt;
import androidx.core.content.ContextCompat;

/**
 * 仅支持Android6.0以上
 */
public class StatusBarUtil {
    public static void setCustomStatusBarHeight(Context context, View view) {
        setCustomStatusBarHeight(context, view, false);
    }

    public static void setCustomStatusBarHeight(Context context, View view, boolean checkMultiWindow) {
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        if (checkMultiWindow && ActivityUtils.isInMultiWindowMode(context))
            layoutParams.height = 1;
        else
            layoutParams.height = GenericTools.getStatusBarHeight(context);
        view.setLayoutParams(layoutParams);
    }

    /**
     * 设置状态栏颜色
     */
    public static void setStatusBar(Activity activity, @ColorInt int color, boolean isDarkMode) {
        // 设置状态栏底色颜色
        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        activity.getWindow().setStatusBarColor(color);

        // 如果亮色，设置状态栏文字为黑色
        if (isDarkMode) {
            activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        } else {
            activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
        }
    }

    //设置全屏状态--隐藏StatusBar/navigationBar
    public static void setFullScreen(Activity aty) {
        aty.getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);

        //适配刘海屏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            WindowManager.LayoutParams localLayoutParams = aty.getWindow().getAttributes();
            localLayoutParams.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
        }
    }

    public static boolean isFullScreen(Activity aty){
        return (aty.getWindow().getDecorView().getSystemUiVisibility() & View.SYSTEM_UI_FLAG_FULLSCREEN)
                == View.SYSTEM_UI_FLAG_FULLSCREEN;
    }

    /**
     * 设置状态栏是否可见
     */
    public static void setStatusBarVisibility(Activity activity, boolean visible) {
        int flag = visible ? 0 : WindowManager.LayoutParams.FLAG_FULLSCREEN;
        activity.getWindow().setFlags(flag, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    // 设置布局全屏顶上StatusBar，设置statusBar的背景颜色为透明且字体为黑色系
    public static void setTransparentStatusBar(Activity aty) {
        setTransparentStatusBar(aty, true);
    }

    // 设置布局全屏顶上StatusBar，设置statusBar的背景颜色为透明且字体为黑色系
    // activity的顶层布局添加android:fitsSystemWindows="true"，否则软键盘的弹窗会受到影响
    // android:fitsSystemWindows="true"会使顶层布局向上留白留出statusBar的空间
    // 动态设置false的时候还需要清除paddingTop：
    // view.setFitsSystemWindows(false);
    // view.setPadding(view.getPaddingLeft(), 0, view.getPaddingRight(), view.getPaddingBottom());
    public static void setTransparentStatusBar(Activity aty, boolean isDarkMode) {
        Window window = aty.getWindow();
        if (isDarkMode) {
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        } else {
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        }
        window.setStatusBarColor(ContextCompat.getColor(aty, android.R.color.transparent));
    }
}